package com.cqgc.service.impl;

import com.cqgc.exception.DetailGoodsException;
import com.cqgc.exception.UpdateGoodsException;
import com.cqgc.mapper.GoodsMapper;
import com.cqgc.pojo.dto.GoodsDTO.AddGoodsDTO;
import com.cqgc.pojo.dto.GoodsDTO.GoodsPageQueryDTO;
import com.cqgc.pojo.dto.GoodsDTO.LikeGoods;
import com.cqgc.pojo.dto.GoodsDTO.UpdateGoodsDTO;
import com.cqgc.pojo.entity.*;
import com.cqgc.service.GoodsService;
import com.cqgc.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public PageResult list(GoodsPageQueryDTO goodsPageQueryDTO) {
        PageHelper.startPage(goodsPageQueryDTO.getPageNum(), goodsPageQueryDTO.getPageSize());
        Page<Goods> page = goodsMapper.list(goodsPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }
    @Override
    public int delete(int gid) {
        return goodsMapper.delete(gid);
    }
    @Override
    public int update(UpdateGoodsDTO updateGoodsDTO) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(updateGoodsDTO,goods);
        Map<String,Object> map = ThreadLocalUtil.get();
//        System.out.println(map+"sss555ss");
        Integer uid = (Integer) map.get("uid");
//        System.out.println(uid+"01111");
        goods.setUpdateUser(uid);
        goods.setUpdateTime(LocalDateTime.now());
        int role = findRoleByUid(uid);
        if (role==1 || role == 2){
            //1.管理员 2.提供者
            return goodsMapper.update(goods);
        }else if (role==3){
            //3.消费者
            throw new UpdateGoodsException("你不能进行修改");
        }
        throw new UpdateGoodsException("你不能进行修改");
    }

    @Override
    public List<Goods> detail() {

        Map<String,Object> map = ThreadLocalUtil.get();
//        System.out.println(map+"sss555ss");
        Integer uid = (Integer) map.get("uid");
        int role = findRoleByUid(uid);
//        String account = (String) map.get("account");
        if (role==2){
            //提供者
            return goodsMapper.detail(uid);
        }else if (role==3 || role==1){
            //消费者
            return goodsMapper.findAll();
        }
        throw new DetailGoodsException("你无权查看");
    }
    @Override
    public int add(AddGoodsDTO addGoodsDTO) {

        Goods goods = new Goods();
        BeanUtils.copyProperties(addGoodsDTO,goods);
//        登录时添加
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer uid = (Integer) map.get("uid");
        goods.setCreateUser(uid);
        goods.setUpdateUser(uid);

        return goodsMapper.add(goods);
    }

    @Override
    public List<Goods> fingByCategoryId(int categoryId) {

        return goodsMapper.findByCategoryId(categoryId);
    }

    @Override
    public List<Goods> like(LikeGoods likeGoods) {
        return goodsMapper.like(likeGoods);
    }

    public int findRoleByUid(int uid) {
        return goodsMapper.findRoleByUid(uid);
    }


}
